package hz.mobileproject.pda.model.attendancemanager.activity;

import android.widget.LinearLayout;

import com.bin.david.form.core.SmartTable;
import com.bin.david.form.data.column.Column;
import com.bin.david.form.data.table.ArrayTableData;
import com.bin.david.form.data.table.MapTableData;
import com.bin.david.form.data.table.TableData;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import hz.mobileproject.pda.R;
import hz.mobileproject.pda.app.MainApp;
import hz.mobileproject.pda.entity.response.Response_loadPoliceInfo;
import hz.mobileproject.pda.listener.CustomNetCallBack;
import hz.mobileproject.pda.model.attendancemanager.bean.Item_SchedulingTable;
import hz.mobileproject.pda.model.attendancemanager.bean.Rp_SchedulingQueryData;
import hz.mobileproject.pda.model.attendancemanager.bean.Rp_queryOutReport;
import hz.mobileproject.pda.model.attendancemanager.bean.Rq_queryDuty;
import hz.mobileproject.pda.model.attendancemanager.bean.SchedulingTableData;
import hz.mobileproject.pda.network.Constance;
import hz.mobileproject.pda.ui.base.BaseActivity;
import hz.mobileproject.pda.utils.HttpRqUtils;

@ContentView(R.layout.activity_scheduling_msg)
public class SchedulingMsgActivity extends BaseActivity {
    @ViewInject(R.id.smartTable)
    private SmartTable smartTable;
    @ViewInject(R.id.ll_SmartTable)
    private LinearLayout ll_SmartTable;

    @Override
    protected void initData() {
        smartTable.setZoom(true); //是否缩放
        queryDuty();
    }


    private void queryDuty() {
        Rq_queryDuty rq = new Rq_queryDuty();
        rq.setCusNumber(MainApp.getInstance().getLoginUser().getCus_number() + "");
        rq.setDeptId(MainApp.getInstance().getLoginUser().getDepartment_id() + "");
        HttpRqUtils.httpPost(Constance.BASE_URL + Constance.URL_OUTGOING_QUERY_DUTY, rq, new CustomNetCallBack() {
            @Override
            public void onSuccessFromSer(int rtnCode, String rtnMsg, String data) {
                LogUtils.e(data);
                Rp_SchedulingQueryData rp = mGson.fromJson(data, Rp_SchedulingQueryData.class);
                Column<String>[] columnsP = new Column[rp.pList.size()];
                int c = 1;
                for (int i = 0; i < rp.pList.size(); i++) {
                    if (rp.pList.get(i).cList == null || rp.pList.get(i).cList.size() == 0) {
                        columnsP[i] = new Column<String>(rp.pList.get(i).name, "c" + c + "");
                        c++;
                    } else {
                        Column<String>[] columnsC = new Column[rp.pList.get(i).cList.size()];
                        for (int j = 0; j < rp.pList.get(i).cList.size(); j++) {
                            columnsC[j] = new Column<String>(rp.pList.get(i).cList.get(j).name, "c" + c + "");
                            c++;
                        }
                        columnsP[i] = new Column<String>(rp.pList.get(i).name, columnsC);
                    }
                }
                List<SchedulingTableData> tableDataList = new ArrayList<>();
                for (int i = 0; i < rp.tableData.size(); i++) {
                    SchedulingTableData std = new SchedulingTableData();
                    for (int j = 0; j < rp.tableData.get(i).size(); j++) {
                        try {
                            Field field = std.getClass().getField("c" + (j + 1));
                            field.set(std, rp.tableData.get(i).get(j));
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    tableDataList.add(std);
                }
                TableData<SchedulingTableData> tableData = new TableData<>(rp.tableName, tableDataList, columnsP);
                smartTable.setTableData(tableData);
                smartTable.getConfig().setMinTableWidth(ll_SmartTable.getWidth()); //设置最小宽度
            }

            @Override
            public void onErrorFromSer(int rtnCode, String rtnMsg) {
                LogUtils.e(rtnCode + rtnMsg);
                ToastUtils.showShort("获取数据失败(" + rtnCode + "," + rtnMsg + ")");
            }

            @Override
            public void onErrorFromNet(Throwable ex, boolean isOnCallback) {
                LogUtils.e(ex.toString());
                ToastUtils.showShort("获取数据失败，请检查网络");
            }
        });
    }
}
